package disruptor;

import netty.util.StartAndStop;
import org.apache.commons.lang3.RandomUtils;
import org.jctools.maps.NonBlockingHashMap;
import test.PerformanceAnalyze;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class MpscMapTest extends StartAndStop {


    int size = 30000;

    ScheduledExecutorService executor = Executors.newScheduledThreadPool(10);

    NonBlockingHashMap<Long, Long> nonBlockingHashMap = new NonBlockingHashMap<>(size);
    ConcurrentHashMap<Long, Long> concurrentHashMap = new ConcurrentHashMap<>(size);

    PerformanceAnalyze performanceAnalyze = new PerformanceAnalyze();

    public void start() throws InterruptedException {

        for (int i = 0; i < 3; i++) {
            executor.execute(() -> {
                while (running) {
                    nonBlockingHashMap.get(RandomUtils.nextLong(0, size));
                }
            });
        }


        for (int i = 0; i < 1; i++) {
            executor.execute(() -> {
                while (running) {
                    long kv = RandomUtils.nextLong(0, size);
                    long now = System.nanoTime();
                    nonBlockingHashMap.put(kv, kv);
                    performanceAnalyze.delay(System.nanoTime() - now);
                    performanceAnalyze.meter();
                }
            });
        }

    }

    public static void main(String[] args) throws InterruptedException {
        new MpscMapTest().start();
        new CountDownLatch(1).await();
    }

}
